/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.run;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.CellValue;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import com.bluestone.util.Util;

/**
 * @author <a href="mailto:bluestone.daniel.q@gmail.com">daniel.q</a>
 * 
 */
public class ProjectReport {

	private ArrayList testsuiteReports = new ArrayList();

	public ArrayList getTestsuitereports() {
		return testsuiteReports;
	}

	public void outputReport() {
		for (int i = 0; i < testsuiteReports.size(); i++) {
			TestSuiteReport temp = (TestSuiteReport) testsuiteReports.get(i);
			ouputReport(temp);
		}
	}

	private void ouputReport(TestSuiteReport testsuiteReport) {
		try {
			if(testsuiteReport.getReportFile()==null){
				return;
			}
			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(
					testsuiteReport.getReportFile()));
			HSSFWorkbook workbook = new HSSFWorkbook(fs);//

			HashMap maps = testsuiteReport.getTestcaseReports();
			Set testcaseReports = maps.entrySet();
			Iterator iterator = testcaseReports.iterator();
			while (iterator.hasNext()) {
				Entry entry = (Entry)iterator.next();
				TestCaseReport testCaseReport = (TestCaseReport) entry.getValue();
				this.reportTeseCase(workbook, testCaseReport);
			}
			java.io.FileOutputStream out = new java.io.FileOutputStream(
					testsuiteReport.getReportFile());
			workbook.write(out);
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			Util.error(e);
		}
	}

	private void reportTeseCase(HSSFWorkbook workbook,
			TestCaseReport testCaseReport) {
		String id = testCaseReport.getId();
		if(id==null||id.indexOf("-")<0){
			return;
		}
		int splitIndex = id.indexOf("-");
		String reportSheetName = id.substring(0, splitIndex);
		String reportRowNum = id.substring(splitIndex+1);

		for (int numSheets = 0; numSheets < workbook.getNumberOfSheets(); numSheets++) {
			if (workbook.getSheetAt(numSheets) == null) {
				return;
			}
			HSSFSheet aSheet = workbook.getSheetAt(numSheets);
			String sheetName = workbook.getSheetName(numSheets);
			if (!reportSheetName.equals(sheetName)) {
				continue;
			}
			HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(aSheet,
					workbook);
			for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) {
				if (aSheet.getRow(rowNumOfSheet) == null) {
					return;
				}
				HSSFRow aRow = aSheet.getRow(rowNumOfSheet);
				evaluator.setCurrentRow(aRow);
				if (null != aRow.getCell(0)) {
					HSSFCell aCell = aRow.getCell(0);
					HSSFFormulaEvaluator.CellValue cellValue = evaluator
							.evaluate(aCell);
					if (HSSFCell.CELL_TYPE_NUMERIC == cellValue.getCellType()) {
						double value = cellValue.getNumberValue();
						if (Double.valueOf(reportRowNum).doubleValue() == value) {
							aCell =aRow.getCell(3);
							if(aCell==null){
								aCell = aRow.createCell((short) 3);	
							}
							aCell.setCellValue(new HSSFRichTextString(
									testCaseReport.getExecuteResult()));
							aCell=aRow.getCell(4);
							if(aCell==null){
								aCell = aRow.createCell((short) 4);
							}
							aCell.setCellValue(new HSSFRichTextString(
									testCaseReport.getComment()));
							return;
						}
					}
				}
			}
		}
	}

	public static void main(String[] args) {
		String xls = "testcase\\Canned_Reports_CheckList_FP3_report.xls";
		try {
			POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(xls));
			HSSFWorkbook workbook = new HSSFWorkbook(fs);
			int numberOfSheets = workbook.getNumberOfSheets();
			for (int i = 0; i < numberOfSheets; i++) {
				HSSFSheet aSheet = workbook.getSheetAt(i);
				if (aSheet != null) {
					HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(
							aSheet, workbook);
					String sheetName = workbook.getSheetName(i);
					for (int j = 0; j < aSheet.getLastRowNum(); j++) {
						HSSFRow aRow = aSheet.getRow(j);
						if (aRow != null) {
							evaluator.setCurrentRow(aRow);
							int cellNum = aRow.getLastCellNum();
							System.out.println("sheet at :" + sheetName
									+ ";row at :" + j + ";cellNum is "
									+ cellNum);
							HSSFCell aCell = aRow.getCell(0);
							if (aCell != null) {
								CellValue cellValue = evaluator.evaluate(aCell);
								HSSFRichTextString textStr = cellValue
										.getRichTextStringValue();
								String str = textStr == null ? " " : textStr
										.getString();
								System.out.println("cell value is :" + str);
							}
						}
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
